package com.immer.session.request;

import com.immer.session.RedisSession;
import com.immer.session.RedisSessionManger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class RedisClusterRequestWrapper extends HttpServletRequestWrapper {

    private HttpServletResponse response;
    private RedisSession httpSession;

    private RequestEventSubject requestEventSubject;
    private RedisSessionManger sessionCacheManger;

    public RedisClusterRequestWrapper(HttpServletRequest request,HttpServletResponse response){
        super(request);
        this.response = response;
    }
    public void setEventObserver(RequestEventSubject subject) {
        this.requestEventSubject = subject;
    }

    public void setSessionCacheManger(RedisSessionManger sessionCacheManger) {
        this.sessionCacheManger = sessionCacheManger;
    }

    @Override
    public HttpSession getSession(boolean create) {
        if (httpSession != null && !httpSession.getExpired()) {
            return httpSession;
        }
        httpSession = sessionCacheManger.createSession(this, response, requestEventSubject, create);
        return httpSession;
    }

    @Override
    public HttpSession getSession() {
        return getSession(true);
    }

}
